package Telas;

import Conexao.ConectaBD;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Tela_Login extends javax.swing.JFrame {

    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    public Tela_Login() throws ClassNotFoundException {
        initComponents();
        setSize(400, 310);
        setLocationRelativeTo(null);
        con = ConectaBD.conectabd();
        Icon();        
    }

//<----------------------------------------------------------------------------------------------------------------------------------------->    

    private void Icon(){
    
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("assistec_icon.png")));
        
    }    
    
//<----------------------------------------------------------------------------------------------------------------------------------------->    

    public void Logar() throws ClassNotFoundException {
        String sql = "Select *from login where Usuario = ? and Senha = ? and Permissao = ?";

        if(tf_usuario.getText().equals("")){
        JOptionPane.showMessageDialog(null, "Campo Usuário é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
        return;
        }
        if(jp_senha.getText().equals("")){
        JOptionPane.showMessageDialog(null, "Campo Senha é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
        return;
        }
        
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, tf_usuario.getText());
            pst.setString(2, jp_senha.getText());
            pst.setString(3, (String) cb_permissao.getSelectedItem());

            rs = pst.executeQuery();

            if (rs.next()) {
                
                if(cb_permissao.getSelectedItem() == "ADMINISTRADOR")
                {
                    Tela_Principal tp = new Tela_Principal(tf_usuario.getText());
                    tp.setVisible(true);
                    dispose();
                }                
                else if(cb_permissao.getSelectedItem() == "BÁSICO")
                {
                    Tela_PrincipalBasico tps = new Tela_PrincipalBasico(tf_usuario.getText());
                    tps.setVisible(true);
                    dispose();
                }
                else if(cb_permissao.getSelectedItem() == "TÉCNICO")
                {
                    Tela_PrincipalTecnico tpc = new Tela_PrincipalTecnico(tf_usuario.getText());
                    tpc.setVisible(true);
                    dispose();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Usuário e Senha invalidos", "Aviso", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException error) {
            JOptionPane.showMessageDialog(null, error);
        }
    }
    
//<----------------------------------------------------------------------------------------------------------------------------------------->    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jl_footer = new javax.swing.JLabel();
        js_footer = new javax.swing.JSeparator();
        jp_senha = new javax.swing.JPasswordField();
        jb_entrar = new javax.swing.JButton();
        tf_usuario = new javax.swing.JTextField();
        jl_usuario = new javax.swing.JLabel();
        jl_senha = new javax.swing.JLabel();
        cb_permissao = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jl_fundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("AssisTec - Login");
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/assistec_login2.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 10, 170, 200);

        jl_footer.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jl_footer.setForeground(new java.awt.Color(255, 255, 255));
        jl_footer.setText("AssisTec");
        getContentPane().add(jl_footer);
        jl_footer.setBounds(10, 210, 46, 15);

        js_footer.setForeground(new java.awt.Color(255, 255, 255));
        getContentPane().add(js_footer);
        js_footer.setBounds(70, 220, 330, 10);

        jp_senha.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        jp_senha.setSelectionColor(new java.awt.Color(0, 0, 0));
        jp_senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jp_senhaActionPerformed(evt);
            }
        });
        jp_senha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jp_senhaKeyPressed(evt);
            }
        });
        getContentPane().add(jp_senha);
        jp_senha.setBounds(220, 90, 160, 30);

        jb_entrar.setBackground(new java.awt.Color(255, 255, 255));
        jb_entrar.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jb_entrar.setForeground(new java.awt.Color(78, 78, 78));
        jb_entrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/assistec_check.png"))); // NOI18N
        jb_entrar.setText("ENTRAR");
        jb_entrar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(78, 78, 78)));
        jb_entrar.setBorderPainted(false);
        jb_entrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_entrarActionPerformed(evt);
            }
        });
        getContentPane().add(jb_entrar);
        jb_entrar.setBounds(250, 230, 100, 50);

        tf_usuario.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        tf_usuario.setSelectionColor(new java.awt.Color(0, 0, 0));
        tf_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_usuarioActionPerformed(evt);
            }
        });
        getContentPane().add(tf_usuario);
        tf_usuario.setBounds(220, 30, 160, 30);

        jl_usuario.setBackground(new java.awt.Color(255, 255, 255));
        jl_usuario.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jl_usuario.setForeground(new java.awt.Color(255, 255, 255));
        jl_usuario.setText("USUÁRIO");
        getContentPane().add(jl_usuario);
        jl_usuario.setBounds(220, 10, 54, 14);

        jl_senha.setBackground(new java.awt.Color(255, 255, 255));
        jl_senha.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jl_senha.setForeground(new java.awt.Color(255, 255, 255));
        jl_senha.setText("SENHA");
        getContentPane().add(jl_senha);
        jl_senha.setBounds(220, 70, 41, 14);

        cb_permissao.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cb_permissao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ADMINISTRADOR", "BÁSICO", "TÉCNICO" }));
        cb_permissao.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        cb_permissao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cb_permissaoKeyPressed(evt);
            }
        });
        getContentPane().add(cb_permissao);
        cb_permissao.setBounds(220, 150, 160, 30);

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("NÍVEL");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(220, 130, 60, 14);

        jl_fundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/assistec_fundo.png"))); // NOI18N
        getContentPane().add(jl_fundo);
        jl_fundo.setBounds(0, 0, 400, 300);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jb_entrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_entrarActionPerformed
        try {
            Logar();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jb_entrarActionPerformed

    private void jp_senhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jp_senhaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                Logar();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jp_senhaKeyPressed

    private void jp_senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jp_senhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jp_senhaActionPerformed

    private void tf_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_usuarioActionPerformed

    private void cb_permissaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cb_permissaoKeyPressed
                if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                Logar();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_cb_permissaoKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Tela_Login().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_permissao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jb_entrar;
    private javax.swing.JLabel jl_footer;
    private javax.swing.JLabel jl_fundo;
    private javax.swing.JLabel jl_senha;
    private javax.swing.JLabel jl_usuario;
    private javax.swing.JPasswordField jp_senha;
    private javax.swing.JSeparator js_footer;
    private javax.swing.JTextField tf_usuario;
    // End of variables declaration//GEN-END:variables
}
